// pages/resume/personInfo/personInfo.js
Page({
  data: {
    personInfoData: {},
    sexDefault: true,
    identityDefault:true,
    birthDefault: true,
    regionDefault: true,
    region: ['上海市', '上海市', '徐汇区'],
    sex: ['男', '女'],
    index: 0,
    identity:['在校生','毕业生'],
    identityIdenx:0,
    date: '1990-09-01',
    wrongTip: "内容有误"
  },
  onLoad: function (options) {
    var that = this;
    wx.getStorage({
      key: 'index',
      success: function (res) {
        that.setData({
          index: res.data
        })
      }
    })
    wx.getStorage({
      key: 'identityIndex',
      success: function (res) {
        that.setData({
          identityIndex: res.data
        })
      }
    })
    wx.getStorage({
      key: 'personInfoData',
      success: function (res) {
        console.log(that.data.personInfoData)
        that.setData({
          personInfoData: res.data,
          date: res.data.birth,
          region: res.data.region
        })
        console.log(that.data.personInfoData)
        if (that.data.personInfoData.sex) {
          that.setData({
            sexDefault: false
          })
        }
        if (that.data.personInfoData.identity) {
          that.setData({
            identityDefault: false
          })
        }
        if (that.data.personInfoData.birth) {
          that.setData({
            birthDefault: false
          })
        }
        if (that.data.personInfoData.region) {
          that.setData({
            regionDefault: false
          })
        }
      },
    })
  },
  headerUpload: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({
          headerPath: res.tempFilePaths,
        })
      }
    })
  },
  bindSexChange: function (e) {
    this.setData({
      index: e.detail.value,
      sexDefault: false,
    })
  },
  bindIdentityChange: function (e) {
    this.setData({
      identityIndex: e.detail.value,
      identityDefault: false,
    })
  },
  bindBirthChange: function (e) {
    this.setData({
      date: e.detail.value,
      birthDefault: false,
    })
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value,
      regionDefault: false,
    })
  },
  formSubmit: function (e) {
    var that = this;
    this.setData({
      personInfoData: e.detail.value,
    })
    var regName = /^([\u4e00-\u9fa5]){2,7}$/;
    var regEmail = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    var regPhone = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!regName.test(this.data.personInfoData.name)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的姓名',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (this.data.sexDefault == true) {
      wx.showModal({
        title: '提示',
        content: '请选择您的性别',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (this.data.birthDefault == true) {
      wx.showModal({
        title: '提示',
        content: '请选择您的出生日期',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (this.data.regionDefault == true) {
      wx.showModal({
        title: '提示',
        content: '请选择您的现居住地',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (!regEmail.test(this.data.personInfoData.email)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的邮箱',
        showCancel: false,
        success: function (res) {
        }
      })
    } else if (!regPhone.test(this.data.personInfoData.phone)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号',
        showCancel: false,
        success: function (res) {
        }
      })
    } else {
      wx.setStorage({
        key: 'index',
        data: e.detail.value.sex,
        success: function (res) {
        }
      });
      wx.setStorage({
        key: 'identityIndex',
        data: e.detail.value.identity,
        success: function (res) {
        }
      });
      this.data.personInfoData.sex = this.data.sex[e.detail.value.sex];
      this.data.personInfoData.identity = this.data.identity[e.detail.value.identity];
      wx.setStorage({
        key: 'personInfoData',
        data: this.data.personInfoData,
        success: function (res) {
        }
      });
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        personInfo: currPage.data.personInfoData,
        success: true,
        personInfoModify: true,
      });
      if (that.data.personInfoData.identity == "毕业生") {
        prevPage.setData({
          necessary: true
        })
      } else {
        prevPage.setData({
          necessary: false
        })
      }
      wx.showModal({
        title: '提示',
        content: '信息更新成功，请继续完善简历',
        showCancel: false,
        success: function (res) {
          wx.navigateBack()
        }
      })
    }
  },
})